package com.zelin.web.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zelin.pojo.R;
import com.zelin.pojo.Student;
import com.zelin.pojo.vo.StudentVo;
import com.zelin.service.StudentService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/10-9:14
 * ------------------------------
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;

    @Override
    public void init() throws ServletException {
        //1. 得到webApplicationContext，（作用就是在tomcat中可以访问spring容器）
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        //2. 从spring容器中得到javaBean对象
        studentService = wac.getBean(StudentService.class);

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 得到请求参数
        String cmd = req.getParameter("cmd");
        //2. 根据参数调用不同的方法
        if(StrUtil.isNotBlank(cmd)){
            if("list".equals(cmd)){
                list(req,resp);
            }else if("search".equals(cmd)){
                search(req,resp);
            }else if("add".equals(cmd)){
                add(req,resp);
            }else if("update".equals(cmd)){
                update(req,resp);
            }else if("delete".equals(cmd)){
                delete(req,resp);
            }
        }
    }



    //1. 列表学生
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.1 得到所有学生
        List<Student> all = studentService.findAll();
        //1.2 转换为字符串并输出
        resp.getWriter().println(JSON.toJSONString(all));
        resp.getWriter().flush();
    }

    //2. 条件查询带分页
    private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //2.1 得到分页参数
        int page = Integer.parseInt(req.getParameter("page"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        //2.2 得到分页的查询参数
        String stud = req.getParameter("stud");
        StudentVo studentVo = JSON.parseObject(stud, StudentVo.class);
        //2.3 开始条件查询带分页
        PageInfo<Student> studentPageInfo = studentService.search(page,pageSize,studentVo);
        //2.4 将数据放到R对象中
        R r = R.ok().put("total", studentPageInfo.getTotal()).put("rows", studentPageInfo.getList());
        //2.5 转换为字符串并输出
        resp.getWriter().println(JSON.toJSONString(r));
        resp.getWriter().flush();
    }

    //3. 添加学生
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //3.1 得到学生信息
        String stud = req.getParameter("stud");
        Student student = JSON.parseObject(stud, Student.class);
        //3.2 添加学生
        studentService.insert(student);
        //3.3 转换为字符串并输出
        resp.getWriter().println(JSON.toJSONString(R.ok()));
        resp.getWriter().flush();
    }

    //4. 修改学生
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //4.1 得到学生信息
        String stud = req.getParameter("stud");
        Student student = JSON.parseObject(stud, Student.class);
        //4.2 修改学生
        studentService.update(student);
        //4.3 转换为字符串并输出
        resp.getWriter().println(JSON.toJSONString(R.ok()));
        resp.getWriter().flush();
    }

    //5. 删除学生
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //5.1 得到要删除的学生学号
        String stud = req.getParameter("ids");
        String[] split = stud.split(",");
        for (String id : split) {
            studentService.delete(id);
        }
        //5.2 转换为字符串并输出
        resp.getWriter().println(JSON.toJSONString(R.ok()));
        resp.getWriter().flush();
    }
}
